package com.bee.baselibrary.weight;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bee.baselibrary.R;

import androidx.annotation.Nullable;


/**
 * Created time 2018/4/25.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description : 待删除按钮的EditText
 */
public class DeleteEditTextView extends LinearLayout {

    private Context context;
    private EditText mEditText;
    private ImageView imgDelete;


    private boolean isCanDelete;

    public DeleteEditTextView(Context context) {
        super(context);
        this.context = context;
    }

    public DeleteEditTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public DeleteEditTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initLayout();
    }

    private void initLayout() {
        View view = LayoutInflater.from(context).inflate(R.layout.delete_edit_layout, null);
        this.imgDelete = (ImageView) view.findViewById(R.id.img_delete);
        this.mEditText = (EditText) view.findViewById(R.id.edit_text);

        mEditText.addTextChangedListener(textWatcher);

        imgDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isCanDelete) {
                    mEditText.setText("");
                }
            }
        });
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (TextUtils.isEmpty(mEditText.getText().toString())) {
                imgDelete.setVisibility(View.GONE);
                isCanDelete = false;
            } else {
                imgDelete.setImageResource(R.mipmap.ic_delete_text);
                imgDelete.setVisibility(View.VISIBLE);
                isCanDelete = true;
            }

            if (onTextChangeInterFace != null) {
                //抛出输入文字变化时的监听
                onTextChangeInterFace.onTextChanged(mEditText.getText().toString(), start, before, count);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    /**
     * 获取输入框
     *
     * @return
     */
    public EditText getPhoneEdit() {
        return mEditText;
    }

    /**
     * 获取输入框数据
     *
     * @return
     */
    public String phoneString() {
        return mEditText.getText().toString();
    }


    public interface OnTextChangeInterFace {
        void onTextChanged(String etPhoneTextString, int start, int before, int count);

    }

    private OnTextChangeInterFace onTextChangeInterFace;

    public void setOnTextChangeInterfaceListener(OnTextChangeInterFace listener) {
        onTextChangeInterFace = listener;
    }


}
